//
//  MainTabBar.swift
//  Weibo
//
//  Created by Jet on 16/5/6.
//  Copyright © 2016年 Jet. All rights reserved.
//

import UIKit
/// 自定义TabBar
class MainTabBar: UITabBar {


    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        //fatalError("init(coder:) has not been implemented")
        super.init(coder: aDecoder)
        self.setupUI()
    }
    
    func setupUI(){
        
        self.addSubview(composeBtn)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let w = self.bounds.width/5
        let h = self.bounds.height
        let rect = CGRectMake(0, 0, w, h)
        
        var index :CGFloat = 0
        for subview in subviews {
            
            if subview.isKindOfClass(NSClassFromString("UITabBarButton")!) {
                //修改frame
                subview.frame = CGRectOffset(rect, w*index, 0)
                index++
                if index == 2 {
                    composeBtn.frame = CGRectOffset(rect, w*index, 0)
                    bringSubviewToFront(composeBtn)//把按钮显示在上层
                    index++
                }
            }
        }
        
    }
    
    //懒加载composeBtn按钮
    lazy var composeBtn:UIButton = {
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button") , forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        btn.sizeToFit()//自适应背景图片大小，来调整自身大小
        
        return btn
    }()
}
